<template>
  <div class="user-avator-dropdown">
    <Dropdown @on-click="handleClick">
      <Avatar :src="userAvator"/>
      <Icon :size="18" type="md-arrow-dropdown"></Icon>
      <DropdownMenu slot="list">
        <DropdownItem name="person">个人中心</DropdownItem>
      </DropdownMenu>
      <DropdownMenu slot="list">
        <DropdownItem name="logout">退出登录</DropdownItem>
      </DropdownMenu>
    </Dropdown>
  </div>
</template>

<script>
import './user.less'
import { mapActions } from 'vuex'
export default {
  name: 'User',
  props: {
    userAvator: {
      type: String,
      default: ''
    }
  },
  methods: {
    ...mapActions([
      'handleLogOut'
    ]),
    person () {
      this.$router.push({ name: 'person' })
    },
    logout () {
      this.handleLogOut().then((res) => {
        // this.$router.push({
        //   name: 'login'
        // })
        const data = res.data
        if (data.code === 200) {
          this.$Message.success(data.message)
          location.reload()// 刷新页面
        } else {
          this.$Message.error(data.error)
        }
      }).catch(err => {
        console.log(err)
        this.$Message.error('退出失败')
      })
    },
    message () {
      this.$router.push({
        name: 'message_page'
      })
    },
    handleClick (name) {
      switch (name) {
        case 'logout': this.logout()
          break
        case 'person': this.person()
          break
      }
    }
  }
}
</script>
